<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class coupon_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//发放现金券
	public function send($coupon_id, $member_id) {
		$now = time();
		$sql = "SELECT * FROM jf_cash_coupon WHERE id='{$coupon_id}' AND status=1 
			AND begin_time<='{$now}' AND end_time>='{$now}'";
		$coupon = $this->db->get_row($sql);
		//不允许重复发放
		if ($coupon && !$this->got($coupon_id, $member_id)) {
			$expiring_days = intval($coupon->expiring_days);
			$tomorrow = strtotime(date("Y-m-d", strtotime("tomorrow")));
			$expiring_time =  $tomorrow + (($expiring_days + 1) * 3600 * 24) - 1 ;
			$sql = "INSERT INTO jf_member_cash_coupon(member_id, cash_coupon_id, cash_coupon_name, face_value, expiring_time, add_time, status) 
				VALUES('{$member_id}', '{$coupon->id}', '{$coupon->name}', '{$coupon->face_value}', '{$expiring_time}', '{$now}', '1')";
			$this->db->query($sql);
			$id = intval($this->db->insert_id);
			return $id;
		} else {
			return false;
		}
	}

	//是否已经获得了此优惠券
	public function got($sn, $member_id) {
		$sql = "SELECT count(*) FROM jf_coupon_sn WHERE sn='{$sn}' AND member_id='{$member_id}' AND member_id>'0'";
		$got = intval($this->db->get_var($sql));
		return $got;
	}

	//判断优惠券是否有效，返回优惠券的优惠金额
	public function valid($sn, $coupons) {
		if ($coupons) {
			foreach ($coupons as $key => $coupon) {
				if ($sn==$coupon->sn) return $coupon->coupon_money;
			}
		}
		return 0;
	}

	//使用优惠券
	public function using($sn, $member_id) {
		$sql = "UPDATE jf_coupon_sn SET use_time='".time()."', status='-1' WHERE sn='{$sn}' AND member_id='{$member_id}' AND member_id>'0'";
		$this->db->query($sql);
	}

	//释放使用优惠券
	public function free($sn, $member_id) {
		$sql = "UPDATE jf_coupon_sn SET use_time='0', status='0' WHERE sn='{$sn}' AND member_id='{$member_id}' AND member_id>'0'";
		$this->db->query($sql);
	}

	//获得我的优惠券（在下单时）
	public function coupons($member_id, $price=0, $goods=array()) {
		$now = time();
		$valid_coupons = array();
		$sql = "SELECT cs.coupon_money, cs.sn, cs.coupon_id, c.name, c.begin_time, c.end_time, c.min_price, c.permit_goods 
			FROM jf_coupon_sn cs INNER JOIN jf_coupon c ON cs.coupon_id=c.id 
			WHERE cs.member_id='{$member_id}' AND cs.member_id>'0' AND cs.status='1' 
			AND c.begin_time<='{$now}' AND c.end_time>='{$now}' AND c.status='1' AND c.offline_use='0'
			ORDER BY cs.id DESC";
		$all_coupons = $this->db->get_results($sql);
		if ($all_coupons) {
			foreach ($all_coupons as $k => $coupon) {
				//满足价格范围
				if ($this->in_coupon_price($price, $coupon->min_price)) {
					//优惠券限制了特定商品
					if ($coupon->permit_goods==1) {
						$coupon_goods = $this->get_coupon_goods($coupon->coupon_id);
						if ($this->is_coupon_goods($goods, $coupon_goods)) {
							$valid_coupons[] = $coupon;
						}
					} else {
						$valid_coupons[] = $coupon;
					}
				}
			}
		}
		return $valid_coupons;
	}

	//获取优惠券的商品
	public function get_coupon_goods($coupon_id) {
		$ids = array();
		$sql = "SELECT goods_id FROM jf_coupon_goods WHERE coupon_id='{$coupon_id}'";
		$goods = $this->db->get_results($sql);
		if ($goods) {
			foreach ($goods as $k => $goods) {
				$ids[] = $goods->goods_id;
			}
			$ids = array_unique($ids);
		}
		return $ids;
	}

	//购买的商品是否优惠券范围内的商品
	public function is_coupon_goods($buy_goods, $coupon_goods) {
		if ($buy_goods) {
			foreach ($buy_goods as $k => $goods_id) {
				if (in_array($goods_id, $coupon_goods)) return true;
			}
		}
		return false;
	}

	//优惠券的最低使用价格
	public function in_coupon_price($buy_price, $coupon_price) {
		if ($coupon_price==0) {
			return true;
		} else {
			if ($buy_price >= $coupon_price) {
				return true;
			} else {
				return false;
			}
		}
	}
}
